const {
  ActivityModel
} = require('../../../../../utils/activity-model.js')
const activityModel = new ActivityModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationHeight: 64,
    navigationTop: 26,
    navigationLeft: 10,
    capsuleWidth: 87,
    activityId: '',
    avatarUrl: '',
    memberId: '',
    nickname: '',
    appletMobile: '',
    scanInfo: {},
    rankList: [],
    winnersList: [],
    rankShowMore: false,
    ruleShow: false,
    prizeShow: false,
    totalProvinceCount: 0,
    progressData: {},
    isPrizeData: [],
    isDone:-1,
  },
  goback() {
    wx.navigateBack({
      fail() {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    })
  },
  changeMore() {
    this.setData({
      rankShowMore: !this.data.rankShowMore
    })
  },
  onLoad: function (options) {
    this.setData({
      activityId: options.activityId,
      avatarUrl: options.avatarUrl,
      memberId: options.memberId,
      nickname: options.nickname,
      appletMobile: options.appletMobile
    })
    const navigationBtnObj = wx.getMenuButtonBoundingClientRect()
    wx.getSystemInfo({
      success: res => {
        this.setData({
          navigationHeight: res.statusBarHeight + navigationBtnObj.height + (navigationBtnObj.top - res.statusBarHeight) * 2,
          navigationTop: navigationBtnObj.top,
          navigationLeft: res.windowWidth - navigationBtnObj.right,
          capsuleWidth: navigationBtnObj.width
        })
      }
    })
    this.getMemberInfo()
  },
  getMemberInfo() {
    const merchantId = wx.getStorageSync('merchantId')
    activityModel.getLightWinners({
      merchantId,
      activityId: this.data.activityId
    }).then(data => {
      this.setData({
        winnersList: data.winners,
        isPrizeData: data.winners.filter(item => item.memberId == this.data.memberId) || [],
        isDone:data.isDone
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
    activityModel.getLightList({
      merchantId,
      activityId: this.data.activityId
    }).then(data => {
      this.setData({
        rankList: data.rankList || [],
        totalProvinceCount: data.totalProvinceCount || 0     
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
    this.getLightProgress()
  },
  getLightProgress() {
    const merchantId = wx.getStorageSync('merchantId')
    activityModel.getLightProgress({
      merchantId,
      activityId: this.data.activityId,
      appletMobile: this.data.appletMobile
    }).then(data => {
      this.setData({
        progressData: data
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  closeRule() {
    this.setData({
      ruleShow: false,
      prizeShow: false
    })
  },
  lookRules() {
    this.setData({
      ruleShow: true
    })
  },
  lookPrize(){
    this.setData({
      prizeShow: true
    })
  }
})